import functools
import datetime
import random

from info import db
from info.models import User
from manage import app


def add_test_users():
    users = []
    now = datetime.datetime.now()
    for num in range(0, 10000):
        try:
            user = User()
            user.nick_name = "%011d" % num
            user.mobile = "%011d" % num
            user.password_hash = "pbkdf2:sha256:50000$SgZPAbEj$a253b9220b7a916e03bf27119d401c48ff4a1c81d7e00644e0aaf6f3a8c55829"
            user.last_login = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            users.append(user)
            print(user.mobile)
        except Exception as e:
            print(e)
    with app.app_context():
        db.session.add_all(users)
        db.session.commit()
    print('OK')


# 用这个装饰器装饰的函数的名字相同，都是wrapper
def test(f):
    def wrapper(*args, **kwargs):
        print("dddddd")
        return f(*args, **kwargs)
    return wrapper


# 给装饰器的内函数加上@functools.wraps()的装饰器后则不会改变函数的原名
# 这样在一个装饰器装饰多个视图函数时避免同一个路由针对多个同名的视图函数而报错
def test1(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        print("dddddd")
        return f(*args, **kwargs)
    return wrapper


@test
def num1():
    print("hahhah")


@test
def num2():
    print("lalala")


@test1
def num3():
    print("hahhah")


@test1
def num4():
    print("lalala")

if __name__ == "__main__":
    print(num1.__name__)
    print(num2.__name__)
    print(num3.__name__)
    print(num4.__name__)
    add_test_users()